//
//  MCRechargePayMoneyTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/6/13.
//  Copyright © 2017年 TLYL01. All rights reserved.
//


#import "MCRechargePayMoneyTableViewCell.h"
#import "MCCollectionViewFlowLayout.h"
#import "MCRechargePaymentTypeCollectionViewCell.h"
#import "MCMineCellModel.h"
@interface MCRechargePayMoneyTableViewCell ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic,strong) UILabel* lab_title;
@property (nonatomic,strong) NSMutableArray*marr_Cell;
@end

@implementation MCRechargePayMoneyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor  whiteColor];
    
    /*
     * 请输入充值金额
     */
    _lab_title = [[UILabel alloc]init];
    _lab_title.layer.cornerRadius=5;
    _lab_title.backgroundColor = [UIColor clearColor];
    _lab_title.layer.masksToBounds = YES;
    _lab_title.font = [UIFont systemFontOfSize:15];
    _lab_title.numberOfLines=1;
    _lab_title.textColor=RGB(100, 100, 100);
    _lab_title.text = @"充值金额:(10-50000元)";
    _lab_title.textAlignment=NSTextAlignmentLeft;
    [self addSubview:_lab_title];
    _lab_title.frame=CGRectMake(10, 0, G_SCREENWIDTH-20, 40);
    
    /*
     * 输入框
     */
    _textField = [[UITextField alloc] init];
    _textField.layer.borderColor= RGB(200, 200, 200).CGColor;
    _textField.layer.borderWidth= 1.0f;
    _textField.layer.cornerRadius=10;
    _textField.placeholder=@"请输入充值金额";
    _textField.borderStyle = UITextBorderStyleNone;
    _textField.backgroundColor=[UIColor whiteColor];
    _textField.font = [UIFont systemFontOfSize:15];
    _textField.textColor = [UIColor blackColor];
    _textField.textAlignment = NSTextAlignmentCenter;
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.keyboardType = UIKeyboardTypeNumberPad;
    [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    [self addSubview:_textField];
    _textField.frame=CGRectMake(10, 40, G_SCREENWIDTH-20, 40);
    
    
    [self addSubview:self.collectionView];
    self.collectionView.frame=CGRectMake(0, 90, G_SCREENWIDTH, 90);
    
    
    _marr_Cell=[[NSMutableArray alloc]init];
    [_marr_Cell    removeAllObjects];
    
    
    
}
-(void)textFieldDidChange:(UITextField *)textfield{
    if (_textField.text.length>4) {
        _textField.text =  [_textField.text substringToIndex:5];
    }
}
/*
 * collectionView
 */
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        //创建一个layout布局类
        MCCollectionViewFlowLayout * layout = [[MCCollectionViewFlowLayout alloc]init];
        //设置布局方向为横向流布局
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.dataSource=self;
        _collectionView.delegate=self;
        [_collectionView registerClass:[MCRechargePaymentTypeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MCRechargePaymentTypeCollectionViewCell class])];
    }
    return _collectionView;
}

#pragma mark - <UICollectionViewDataSource>
// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return nil ;
    }
    return nil ;
}
//// 设置section头视图的参考大小，与tableheaderview类似
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    BKIntegralCollectionModel * model=self.marr_Cell[section];
//    return model.header_size;
//}
//设置section的颜色
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.section_color;
}
//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionModel * model=self.marr_Cell[indexPath.section];
    return model.item_size;
}
//设置section的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.section_Edge;
}
// 两个cell之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.interitemSpacing;
}
// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    CollectionModel * model=self.marr_Cell[section];
    return model.lineSpacing;
}

//numberOfItemsInSection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    CollectionModel * model=self.marr_Cell[section];
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCRechargePaymentTypeCollectionViewCell class])]) {
        return self.marr_Cell.count;
    }
    return 0;
}

//numberOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
#pragma mark - /Users/miaocai/Desktop/tl/ios/TLYL/TLYL/Classes/Mine/View/MCRecharge/MCRechargePaymentTypeTableViewCell.m:155:5: nil returned from a method that is expected to return a non-null value
    UICollectionViewCell*  cell=nil;
    CollectionModel * model=self.marr_Cell[indexPath.row];
    if (model.id_dentifier) {
        cell =[collectionView dequeueReusableCellWithReuseIdentifier:model.id_dentifier forIndexPath:indexPath];
    }
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCRechargePaymentTypeCollectionViewCell class])]) {
        
        MCRechargePaymentTypeCollectionViewCell *ex_cell=(MCRechargePaymentTypeCollectionViewCell *)cell;
        
        if (model.userInfo) {
            ex_cell.dataSource=model.userInfo;
        }
        
    }
    
#pragma mark - /Users/miaocai/Desktop/tl/ios/TLYL/TLYL/Classes/Mine/View/MCRecharge/MCRechargePaymentTypeTableViewCell.m:155:5: nil returned from a method that is expected to return a non-null value
    return cell;
}

//didSelect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionModel * model=self.marr_Cell[indexPath.row];
    if ([model.id_dentifier isEqualToString:NSStringFromClass([MCRechargePaymentTypeCollectionViewCell class])]) {
        /*
         * 选择金额
         */
        if ([self.delegate respondsToSelector:@selector(MCRechargePayMoney_ChooseMoney:)]) {
            [self.delegate MCRechargePayMoney_ChooseMoney:model.userInfo];
        }
    }
}


+(CGFloat)computeHeight:(id)info{
    
    return 180;
    
}

-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    
    if (_marr_Cell.count<1) {
        NSArray * arr=dataSource;
        for (int i=0; i<arr.count; i++) {
            CollectionModel * model0=[[CollectionModel alloc]init];
            model0.header_size=CGSizeMake(G_SCREENWIDTH, 40);
            model0.item_size=CGSizeMake((G_SCREENWIDTH-40)/3.0, 30);
            model0.section_color=[UIColor whiteColor];
            model0.section_Edge=UIEdgeInsetsMake(0, 10, 0, 10);
            model0.interitemSpacing=0.1;
            model0.lineSpacing=10;
            model0.isHaveHeader=YES;
            model0.id_dentifier=NSStringFromClass([MCRechargePaymentTypeCollectionViewCell class]);
            /*
             * info
             */
            model0.userInfo=arr[i];
            [_marr_Cell addObject:model0];
        }
        
        [_collectionView reloadData];
       
        
        [self.collectionView  selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

@end



















